#
# Master Makefile
#
# Copyright (C) 2000-2023 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
@MCOMMON@

working_dir=@working_dir@
dir_group=@dir_group@
dir_user=@dir_user@

srcdir =	@srcdir@
VPATH = 	@srcdir@
.PATH:		@srcdir@
topdir = @BUILD_DIR@
thisdir = @BUILD_DIR@


first_rule: all
dummy:

# --client-only directories
fd_subdirs = src scripts src/lib src/findlib src/filed \
       @READLINE_SRC@ @BAT_DIR@ src/console

# Non-client-only directores
subdirs =    src/cats @DIRD_DIR@ @STORED_DIR@ src/tools

all_subdirs = ${fd_subdirs} ${@ALL_DIRS@} manpages

# plugins enabled
pluginsdirs = @FD_PLUGIN_DIR@ @DIR_PLUGIN_DIR@

DIST =	INSTALL README.configure configure Makefile Makefile.in ChangeLog

DIST_CFG = autoconf/aclocal.m4 autoconf/configure.in \
	autoconf/config.h.in  autoconf/acconfig.h  autoconf/Make.common.in \
	autoconf/install-sh autoconf/mkinstalldirs

doc_files = VERIFYING ChangeLog README ReleaseNotes LICENSE \
	    LICENSE-FAQ LICENSE-FOSS INSTALL

MKDIR = $(srcdir)/autoconf/mkinstalldirs
LIBTOOL_DEPS = @LIBTOOL_DEPS@

#-------------------------------------------------------------------------

all: Makefile
	@for I in ${all_subdirs}; \
	  do (cd $$I; echo "==>Entering directory `pwd`"; \
	      $(MAKE) DESTDIR=$(DESTDIR) $@ || (echo ""; echo ""; echo "  ====== Error in `pwd` ======"; \
			    echo ""; echo ""; false ) || false) || exit 1; \
	done

depend:
	@for I in ${all_subdirs}; \
	   do echo "==> Entering directory $$I" ; \
	      $(MAKE) DESTDIR=$(DESTDIR) -C $$I $@ ; done
	@for I in ${pluginsdirs}; \
	   do echo "==> Plugin directory $$I" ; \
	      $(MAKE) DESTDIR=$(DESTDIR) -C $$I $@ ; done

bacula-fd: Makefile
	@for I in ${fd_subdirs}; \
	  do (cd $$I; echo "==>Entering directory `pwd`"; \
	      $(MAKE) DESTDIR=$(DESTDIR) all || \
	      (echo ""; echo ""; echo "   ====== Error in `pwd` ======"; \
	       echo ""; echo ""; false ) || false) || exit 1; \
	done

plugins:
	@for I in ${pluginsdirs}; \
	   do echo "==> Plugin directory" $$I ; \
	      $(MAKE) DESTDIR=$(DESTDIR) -C $$I all ; done

#-------------------------------------------------------------------------

autoconf/aclocal.m4: autoconf/configure.in autoconf/bacula-macros/* autoconf/gettext-macros/* autoconf/libtool/*

#  Note, the following is needed in the above if ever any new macro is added.
#   However, at the current time, the -I libtool causes the autoconf/aclocal.m4
#   get messed up, so this is commented out
#	cd autoconf && aclocal -I bacula-macros -I gettext-macros -I libtool

configure: autoconf/configure.in autoconf/aclocal.m4 autoconf/acconfig.h autoconf/config.h.in
	cd $(srcdir);
	${RMF} config.cache config.log config.out config.status src/config.h
	${RMF} -r autoconf/autom4te.cache autom4te.cache
	autoconf --prepend-include=$(srcdir)/autoconf \
	autoconf/configure.in > configure
	chmod 755 configure
	${RMF} -r autoconf/autom4te.cache autom4te.cache

config.status:
	if test -x config.status; then config.status --recheck; \
	else $(SHELL) configure; fi

autoconf/config.h.in: autoconf/configure.in autoconf/acconfig.h
	cd $(srcdir);
	${RMF} config.cache config.log config.out config.status src/config.h
	autoheader --prepend-include=$(srcdir)/autoconf \
	autoconf/configure.in > autoconf/config.h.in
	chmod 644 autoconf/config.h.in

libtool: Makefile $(LIBTOOL_DEPS)
	$(SHELL) ./config.status --recheck

installdirs:
	$(MKDIR) $(DESTDIR)$(sbindir)
	$(MKDIR) $(DESTDIR)$(sysconfdir)
	chmod 770 $(DESTDIR)$(sysconfdir)
	-if test "x$(dir_user)" != "x" ; then \
	   chown $(dir_user) $(DESTDIR)$(sysconfdir); \
	fi
	-if test "x$(dir_group)" != "x" ; then \
	   chgrp $(dir_group) $(DESTDIR)$(sysconfdir); \
	fi
	$(MKDIR) $(DESTDIR)$(scriptdir)
	chmod 771 $(DESTDIR)$(scriptdir)
	$(MKDIR) $(DESTDIR)$(docdir)
	$(MKDIR) $(DESTDIR)$(archivedir)
	$(MKDIR) $(DESTDIR)$(bsrdir)
	$(MKDIR) $(DESTDIR)${logdir}
	-if test ! -d $(DESTDIR)$(working_dir) ; then \
	   $(MKDIR) $(DESTDIR)$(working_dir); \
	   chmod 770 $(DESTDIR)$(working_dir); \
	fi
	-if test "x$(dir_user)" != "x" ; then \
	   chown $(dir_user) $(DESTDIR)$(working_dir); \
	   chown $(dir_user) $(DESTDIR)$(logdir); \
	fi
	-if test "x$(dir_group)" != "x" ; then \
	   chgrp $(dir_group) $(DESTDIR)$(working_dir); \
	   chgrp $(dir_group) $(DESTDIR)$(logdir); \
	fi

install: installdirs install-plugins
	@for I in $(doc_files) ; do $(INSTALL_DATA) $$I $(DESTDIR)${docdir}; done
	@for I in $(all_subdirs); do (cd $$I && $(MAKE) DESTDIR=$(DESTDIR) $@ || exit 1); done

uninstall:
	@for I in $(all_subdirs); do (cd $$I && $(MAKE) DESTDIR=$(DESTDIR) $@ || exit 1); done

install-autostart:
	(cd platforms && $(MAKE) DESTDIR=$(DESTDIR) $@ || exit 1)

install-autostart-dir:
	(cd platforms && $(MAKE) DESTDIR=$(DESTDIR) $@ || exit 1)

install-autostart-fd:
	(cd platforms && $(MAKE) DESTDIR=$(DESTDIR) $@ || exit 1)

install-autostart-sd:
	(cd platforms && $(MAKE) DESTDIR=$(DESTDIR) $@ || exit 1)

uninstall-autostart:
	(cd platforms && $(MAKE) DESTDIR=$(DESTDIR) $@ || exit 1)

uninstall-autostart-dir:
	(cd platforms && $(MAKE) DESTDIR=$(DESTDIR) $@ || exit 1)

uninstall-autostart-fd:
	(cd platforms && $(MAKE) DESTDIR=$(DESTDIR) $@ || exit 1)

uninstall-autostart-sd:
	(cd platforms && $(MAKE) DESTDIR=$(DESTDIR) $@ || exit 1)

install-plugins:
	@for I in ${pluginsdirs}; \
	   do echo "==> Plugin directory $$I" ; \
	      $(MAKE) DESTDIR=$(DESTDIR) -C $$I install ; done

Makefile: Makefile.in
	cd $(topdir) \
	    && CONFIG_FILES=$(thisdir)/$@ CONFIG_HEADERS= $(SHELL) ./config.status

Makefiles:
	$(SHELL) config.status
	(cd scripts; \
	 chmod 755 bacula sbtraceback mtx-changer bconsole devel_bacula)
	(cd src/cats; \
	 chmod 755 create_bacula_database update_bacula_tables \
	   make_bacula_tables grant_bacula_privileges drop_bacula_tables \
	   drop_bacula_database make_catalog_backup delete_catalog_backup)
	@for I in @DB_BACKENDS@ ; do \
	  (cd src/cats; \
	   chmod 755 create_$${I}_database update_$${I}_tables \
	      make_$${I}_tables grant_$${I}_privileges drop_$${I}_tables \
	      drop_$${I}_database); \
	  done
	(cd src/qt-console; \
	 chmod 755 install_conf_file build-depkgs-qt-console)
	(cd src/qt-console/tray-monitor; \
	 chmod 755 install_conf_file build-depkgs-qt-console)

clean: clean-plugins
	@for I in ${all_subdirs} ; \
	  do (cd $$I; echo "==>Entering directory `pwd`"; ${MAKE} $@ || exit 1); done
	@(cd platforms; echo "==>Entering directory `pwd`"; ${MAKE} $@ || exit 1)
	@$(RMF) *~ 1 2 3 core core.* config.guess console.log console.sum
	@$(RMF) examples/1 examples/2 examples/devices/1 examples/devices/2
	@$(RMF) -r autom4te.cache
	@find . -name ".#*" -exec $(RMF) {} \;

clean-plugins:
	@for I in ${pluginsdirs}; \
	   do echo "==> Plugin directory" $$I; \
	      $(MAKE) DESTDIR=$(DESTDIR) -C $$I clean ; done

# clean for distribution
distclean:
	@for I in $(all_subdirs); do (cd $$I && $(MAKE) $@ || exit 1); done
	@for I in $(all_subdirs); do (cd $$I && $(RMF) startit stopit btraceback); done
	@(cd $(srcdir) && $(RMF) *~ config.cache config.h config.log config.status config.out)
	@(cd $(srcdir) && $(RMF) Makefile autoconf/Make.common)
	@(cd platforms; echo "==>Entering directory `pwd`"; ${MAKE} $@ || exit 1)
	@$(RMF) bacula fd Makefile startmysql stopmysql startit stopit btraceback
	@$(RMF) bconsole gconsole
	@$(RMF) *~ 1 2 3 core core.* config.guess console.log console.sum
	@$(RMF) working/* kerns-*
	@$(RMF) -rf txt diff src/python src/testprogs
	@$(RMF) libtool

devclean:
	@for I in $(all_subdirs); do (cd $$I && $(MAKE) $@ || exit 1); done
	@for I in $(all_subdirs); do (cd $$I && $(RMF) startit stopit btraceback); done
	@(cd $(srcdir) && $(RMF) *~ config.cache config.h config.log config.status config.out)
	@(cd $(srcdir) && $(RMF) Makefile autoconf/Make.common)
	@(cd platforms; echo "==>Entering directory `pwd`"; ${MAKE} $@ || exit 1)
	@$(RMF) bacula fd Makefile startmysql stopmysql startit stopit btraceback
	@$(RMF) bconsole gconsole
	@$(RMF) *~ 1 2 3 core core.* config.guess console.log console.sum
	@$(RMF) working/*

distdirs:
	mkdir ../$(VERNAME);
	mkdir ../$(VERNAME)/autoconf;
	@for I in $(all_subdirs); do (cd $$I && $(MAKE) DESTDIR=$(DESTDIR) $@ || (echo "Failed to make distclean in $$I"; exit 0) ); done

distcopy:
	$(CP) -p $(DIST) ../$(VERNAME);
	$(CP) -p $(DIST_CFG) ../$(VERNAME)/autoconf;
	@for I in $(all_subdirs); do (cd $$I && $(MAKE) DESTDIR=$(DESTDIR) $@ || exit 1); done

distrib: configure autoconf/config.h.in distdirs distcopy

# ------------------------------------------------------------------------
